package org.jeecg.modules.lab.utensil.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.lab.utensil.entity.LabUtensil;
import org.jeecg.modules.lab.utensil.entity.LabUtensilCheck;
import org.jeecg.modules.lab.utensil.entity.LabUtensilChecklist;
import org.jeecg.modules.lab.utensil.mapper.LabUtensilCheckMapper;
import org.jeecg.modules.lab.utensil.mapper.LabUtensilChecklistMapper;
import org.jeecg.modules.lab.utensil.mapper.LabUtensilMapper;
import org.jeecg.modules.lab.utensil.service.ILabUtensilCheckService;
import org.jeecg.modules.lab.utensil.vo.LabUtensilCheckPage;
import org.jeecg.modules.lab.utensil.vo.LabUtensilCheckVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 器具检测申请表
 * @Author: jeecg-boot
 * @Date:   2023-08-22
 * @Version: V1.0
 */
@Service
public class LabUtensilCheckServiceImpl extends ServiceImpl<LabUtensilCheckMapper, LabUtensilCheck> implements ILabUtensilCheckService {

	@Autowired
	private LabUtensilCheckMapper labUtensilCheckMapper;
	@Autowired
	private LabUtensilChecklistMapper labUtensilChecklistMapper;
	@Autowired
	private LabUtensilMapper labUtensilMapper;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(LabUtensilCheckPage labUtensilCheckPage) {

		LabUtensilCheck labUtensilCheck = new LabUtensilCheck();
		BeanUtils.copyProperties(labUtensilCheckPage, labUtensilCheck);
		labUtensilCheck.setCheckState("未提交");
		List<String> list = new ArrayList<>();
		labUtensilCheckPage.getLabUtensilChecklistList().forEach(e->{
			e.setDepartment(labUtensilCheck.getDepartment());
			e.setCheckRef(labUtensilCheck.getCheckRef());
			e.setListState("未提交");
			list.add(e.getUteRef());
		});
		List<LabUtensilChecklist> labUtensilChecklistList = labUtensilCheckPage.getLabUtensilChecklistList();
		labUtensilCheckMapper.insert(labUtensilCheck);
		if(labUtensilChecklistList!=null && labUtensilChecklistList.size()>0) {
			for(LabUtensilChecklist entity:labUtensilChecklistList) {
				//根据出厂编号判断是否存在这个器具
				LabUtensil labUtensil = labUtensilMapper.selectOne(new LambdaQueryWrapper<LabUtensil>().eq(LabUtensil::getManuNumber, entity.getUteRef()));
				if (labUtensil!=null){//存在这个器具
					//外键设置
					entity.setCheckId(labUtensilCheck.getId());
					labUtensilChecklistMapper.insert(entity);
				}else {
					throw new RuntimeException(entity.getUteRef()+"器具不存在");
				}

			}
			//修改器具状态(用于统计)
			labUtensilMapper.updateByManu(list,"检测中");
		}
	}

	/**
	 * 编辑修改
	 * @param labUtensilCheck
	 * @param labUtensilChecklistList
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(LabUtensilCheck labUtensilCheck,List<LabUtensilChecklist> labUtensilChecklistList) {
		labUtensilCheckMapper.updateById(labUtensilCheck);
		List<String> list = new ArrayList<>();
		labUtensilChecklistList.forEach(e->{
			e.setDepartment(labUtensilCheck.getDepartment());
			e.setCheckRef(labUtensilCheck.getCheckRef());
			list.add(e.getUteRef());//获取所有出厂编号
		});
		//1.先删除子表数据
		labUtensilChecklistMapper.deleteByMainId(labUtensilCheck.getId());
		//2.子表数据重新插入
		if(labUtensilChecklistList!=null && labUtensilChecklistList.size()>0) {
			for(LabUtensilChecklist entity:labUtensilChecklistList) {
				//根据出厂编号判断是否存在这个器具
				LabUtensil labUtensil = labUtensilMapper.selectOne(new LambdaQueryWrapper<LabUtensil>().eq(LabUtensil::getManuNumber, entity.getUteRef()));
				if (labUtensil!=null) {//存在这个器具
					//外键设置
					entity.setCheckId(labUtensilCheck.getId());
					labUtensilChecklistMapper.insert(entity);
				}else {
					throw new RuntimeException(entity.getUteRef()+"器具不存在");
				}
			}
			//修改器具状态(用于统计)
			labUtensilMapper.updateByManu(list,"检测中");
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		labUtensilChecklistMapper.deleteByMainId(id);
		labUtensilCheckMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			labUtensilChecklistMapper.deleteByMainId(id.toString());
			labUtensilCheckMapper.deleteById(id);
		}
	}


	//保存器具检测单
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveCheck(LabUtensilCheckVo labUtensilCheckVo) {
		LabUtensilCheck labUtensilCheck = new LabUtensilCheck();
		BeanUtils.copyProperties(labUtensilCheckVo,labUtensilCheck);
       labUtensilCheckMapper.insert(labUtensilCheck);
		List<LabUtensilChecklist> checkList = labUtensilCheckVo.getCheckList();

		checkList.forEach(e->{
			e.setCheckId(labUtensilCheck.getId());
			e.setListState("待检验");
			labUtensilChecklistMapper.insert(e);
//			LabUtensil labUtensil = new LabUtensil();
//			labUtensil.setManuNumber(e.getUteRef());
//			labUtensil.setStatus("检定中");
//			labUtensilMapper.update(labUtensil,new LambdaQueryWrapper<LabUtensil>().eq(LabUtensil::getManuNumber,labUtensil.getManuNumber()));
		});
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delete(String id) {
		labUtensilCheckMapper.deleteById(id);
		labUtensilChecklistMapper.delete(new LambdaQueryWrapper<LabUtensilChecklist>().eq(LabUtensilChecklist::getCheckId,id));
	}
}
